﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Threading;
using MsgEntity;

namespace CommunicationLib
{

    /*处理返回报文*/

    public class ReciveMsgOper:Communicator
    {


        public ReciveMsg PacketCheck(byte[] data, int len)
        {
            if (data.Length != len)
                throw new Exception("报文长度不正确！");
            int datalength = data.Length;
            List<byte> dataContentBytes = new List<byte>();
            for (int i = 0; i < data.Length - 2; i++)
            {
                dataContentBytes.Add(data[i]);
            }
            byte[] crcCode = BigToLittle(BitConverter.GetBytes(Crc16(dataContentBytes.ToArray())));
            if (data[data.Length - 1] != crcCode[2] || data[data.Length - 2] != crcCode[3])
                throw new Exception("报文CRC校验不正确！");
            if (data[0] != 0xAA || data[1] != 0x55)
                throw new Exception("报文头校验不正确！");
            if (data[2] != 0x02)
                throw new Exception("报文版本校验不正确！");
            if (data[3] != len)
                throw new Exception("报文长度不校验不正确！");
            switch (data[5])
            {
                case 0x38:
                    return data[14] == 0x00 ? ReciveMsg.AuthSuccess : ReciveMsg.AuthFail;
                    break;
                case 0x41:
                    return data[14] == 0x00 ? ReciveMsg.DataUpSuccess : ReciveMsg.DataUpFail;
                    break;
                default:
                    throw new Exception("不能识别的报文类型！");
                    break;
            }
        }


    }
}